/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.gui.mvc.controller.testdesign;

import org.testis.data.AtofModuleConstants;
import org.testis.gui.mvc.controller.AbstractController;
import org.testis.gui.mvc.controller.TestDesignControllerList;
import org.testis.gui.mvc.model.testdesign.SelectSuiteModel;
import org.testis.gui.mvc.view.testdesign.SelectSuiteView;
import org.testis.utils.UserSettingsManager;
import org.testis.utils.PropertyFileProvider;
import java.util.LinkedList;

/**
 *
 * @author selcuk
 */
public class SelectSuiteController extends AbstractController {

    public static final String LAST_LOADED_TESTSUITES_PROPERTY = "LastLoadedTestSuites";
    public static final String LAST_LOADED_DYNAMICMAPS_PROPERTY = "LastLoadedDynamicMaps";
    public static final String SELECTED_TESTSUITES_PROPERTY = "SelectedTestSuites";
    public static final String SELECTED_DYNAMICMAPS_PROPERTY = "SelectedDynamicMaps";
    public static final String VIEWVISIBILITY_PROPERTY = "ViewVisibility";

    public SelectSuiteController() {
        super();
    }

    public void startLoader() {
        initialize();
        setModelProperty(VIEWVISIBILITY_PROPERTY, true);
    }

    public void operationCloseWindow() {
        super.cleanAll();
    }

    public void initialize() {
        addModel(new SelectSuiteModel());
        SelectSuiteView view = new SelectSuiteView(TestDesignControllerList.newInstance().
                getParentFrame(), true);
        view.pack();
        addView(view);

        LinkedList<String> suiteList = new LinkedList<String>();
        LinkedList<String> dynamicMapList = new LinkedList<String>();
        readPropertyFile(suiteList, dynamicMapList);
        operationSetLastLoadedDynamicMaps(dynamicMapList);
        operationSetLastLoadedTestSuites(suiteList);
    }

    private void readPropertyFile(LinkedList<String> suiteList, LinkedList<String> dynamicMapList) {
        try {
            /*loading last loaded  tree*/

            String val = UserSettingsManager.getInstance().getSelectedSuites();
            if (val != null && !val.trim().equals("")) {
                String[] loadedTestDesigns = val.split("#");
                System.out.println(
                        "Test tree count to be loaded:" + loadedTestDesigns.length);
                for (int i = 0; i < loadedTestDesigns.length; i++) {
                    suiteList.add(loadedTestDesigns[i]);
                }
            }


            val = UserSettingsManager.getInstance().getSelectedDynamicMaps();
            if (val != null && !val.equals("")) {
                String[] loadedDynamicMaps = val.split("#");
                System.out.println(
                        "Dynamic map tree count to be loaded:" + loadedDynamicMaps.length);
                for (int i = 0; i < loadedDynamicMaps.length; i++) {
                    dynamicMapList.add(loadedDynamicMaps[i]);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void operationSetLastLoadedTestSuites(LinkedList<String> input) {
        setModelProperty(LAST_LOADED_TESTSUITES_PROPERTY, input);
    }

    public void operationSetLastLoadedDynamicMaps(LinkedList<String> input) {
        setModelProperty(LAST_LOADED_DYNAMICMAPS_PROPERTY, input);
    }

    public void operationSetSelectedTestSuites(LinkedList<String> input) {
        setModelProperty(SELECTED_TESTSUITES_PROPERTY, input);
    }

    public void operationSetSelectedDynamicMaps(LinkedList<String> input) {
        setModelProperty(SELECTED_DYNAMICMAPS_PROPERTY, input);
    }

    public LinkedList<String> getSelectedTestSuites() {
        return (LinkedList<String>) getModelProperty(
                SELECTED_TESTSUITES_PROPERTY);
    }

    public LinkedList<String> getSelectedDynamicMaps() {
        return (LinkedList<String>) getModelProperty(
                SELECTED_DYNAMICMAPS_PROPERTY);
    }
}
